//
//  LYLEditingViewF.m
//  Template
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 LYL. All rights reserved.
//

#import "LYLEditingViewF.h"

#define kSelfViewW self.frame.size.width
#define kSelfViewH self.frame.size.height
#define kSpace 10
#define kImageSpace 5
#define kTopImgViewH (kSelfViewH - kImageSpace)*0.6
#define kNameFieldH (kSelfViewH - kImageSpace)*0.0625
#define kDateLabelW 130
#define kDateLabelH (kSelfViewH - kImageSpace)*0.028
#define kPlaceLabelH (kSelfViewH - kImageSpace)*0.028
#define kLineViewH (kSelfViewH - kImageSpace)*0.05
#define kLineOrigineY (kSelfViewH - kImageSpace)*0.015
#define kBodyViewH (kSelfViewH - kImageSpace)*0.21

@implementation LYLEditingViewF

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews {
    // 适配
    NSInteger dateAndPlaceLabelFont = 11;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        dateAndPlaceLabelFont = 10;
    }else if ([WYTDevicesTool iPhone320_DEVICES]){
        dateAndPlaceLabelFont = 9;
    }

    self.topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kImageSpace, kImageSpace, kSelfViewW - 2*kImageSpace, kTopImgViewH)];
    //self.topImgView.backgroundColor = [UIColor grayColor];
    self.topImgView.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVF"].CGImage;
    self.topImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.topImgView.layer.masksToBounds = YES;
    [self addSubview:self.topImgView];
    
    self.touchImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.touchImageButton.frame = CGRectMake(kImageSpace, kImageSpace,kSelfViewW - 2*kImageSpace, kTopImgViewH);
    [self addSubview:self.touchImageButton];
    
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(kSpace, kTopImgViewH + kImageSpace, kSelfViewW - kSpace*2, kNameFieldH)];
//    self.nameField.backgroundColor = [UIColor orangeColor];
    self.nameField.font = [UIFont systemFontOfSize:20];
    self.nameField.placeholder = @"添加标题";
    [self addSubview:self.nameField];
    
    self.weatherImg = [[UIImageView alloc] initWithFrame:CGRectMake(kSpace, kTopImgViewH + kNameFieldH + kImageSpace, kDateLabelH, kDateLabelH)];
    self.weatherImg.image = [UIImage imageNamed:@"heavyRain_gray"];
//    self.weatherImg.backgroundColor = [UIColor blackColor];
    [self addSubview:self.weatherImg];
    
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSpace + kDateLabelH, kTopImgViewH + kNameFieldH + kImageSpace, kSelfViewW - kSpace - kDateLabelH, kDateLabelH)];
//        self.dateLabel.backgroundColor = [UIColor grayColor];
    self.dateLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    self.dateLabel.text = @"2015/10/31 12:09pm";
    self.dateLabel.font = [UIFont systemFontOfSize:dateAndPlaceLabelFont];
    [self addSubview:self.dateLabel];
    
    self.placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSpace, kTopImgViewH + kNameFieldH + kDateLabelH + kImageSpace, kSelfViewW - kSpace, kPlaceLabelH)];
//    self.placeLabel.backgroundColor = [UIColor orangeColor];
    self.placeLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    self.placeLabel.text = @"北京市, 海淀区";
    self.placeLabel.font = [UIFont systemFontOfSize:dateAndPlaceLabelFont];
    [self addSubview:self.placeLabel];
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(kSpace, kTopImgViewH + kNameFieldH + kDateLabelH + kPlaceLabelH +  + kImageSpace + kLineOrigineY, kSelfViewW*0.6, 1)];
    self.lineView.backgroundColor = [UIColor colorWithWhite:0.311 alpha:0.500];
    [self addSubview:self.lineView];
    
    self.bodyView = [[LYLPlaceHoderTextView alloc] initWithFrame:CGRectMake(kSpace, kTopImgViewH + kNameFieldH + kDateLabelH + kPlaceLabelH + kLineViewH + kImageSpace, kSelfViewW - kSpace*2,kBodyViewH)];
    self.bodyView.backgroundColor = [UIColor clearColor];
    self.bodyView.placeholder = @"写点什么...";
    self.bodyView.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.bodyView];
    
    UISwipeGestureRecognizer *swipToHideKeyboard = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipToHideKeyboardChick:)];
    swipToHideKeyboard.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipToHideKeyboard];
    UISwipeGestureRecognizer *textViewSwipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(textViewHasBeenSwipeUp:)];
    textViewSwipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:textViewSwipeUp];
    // 只作用于date,weather,location
    UIButton *presentToDiaryinfoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    presentToDiaryinfoBtn.frame = CGRectMake(0, kTopImgViewH + kNameFieldH + kImageSpace, kSelfViewW, kDateLabelH + kPlaceLabelH);
    presentToDiaryinfoBtn.backgroundColor = [UIColor clearColor];
    [presentToDiaryinfoBtn addTarget:self action:@selector(chickPresentToDiaryinfoBtn) forControlEvents:1<<6];
    [self addSubview:presentToDiaryinfoBtn];
    
}

- (void)chickPresentToDiaryinfoBtn
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"TAPTOJUMPINTODIARYINFOVIEW" object:nil userInfo:nil];
}
- (void)textViewHasBeenSwipeUp:(UISwipeGestureRecognizer *)sender{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPEUPTODESK" object:nil userInfo:nil];
    
    
}
- (void)swipToHideKeyboardChick:(UISwipeGestureRecognizer *)sender
{
    [self endEditing:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPETOHIDEKEYBOARD" object:nil userInfo:nil];
}

@end
